package cn.stylefeng.guns.modular.business.controller;

import cn.stylefeng.guns.modular.business.reqeust.SampleDataRequest;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.enums.ResBizTypeEnum;
import cn.stylefeng.guns.modular.business.entity.SampleData;
import cn.stylefeng.guns.modular.business.service.ISampleDataService;
import cn.stylefeng.roses.kernel.rule.pojo.request.BaseRequest;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import java.util.List;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  样本数据前端控制器
 * </p>
 *
 * @author generator
 * @since 2024-07-11
 */
@RestController
@ApiResource(path = "/api/service/sampleData", name = "样本数据", resBizType = ResBizTypeEnum.BUSINESS)
public class SampleDataController {

    @Autowired
    private ISampleDataService sampleDataService;

    @PostResource(name = "样本数据-新增", path = "/add")
    public ResponseData<?> add(@RequestBody @Validated(BaseRequest.add.class) SampleDataRequest sampleDataRequest){
        SampleData sampleData = new SampleData();
        BeanUtils.copyProperties(sampleDataRequest, sampleData);
       sampleDataService.saveExt(sampleData);
        return new SuccessResponseData<>();
   }

    @PostResource(name = "样本数据-删除", path = "/delete")
    public SuccessResponseData<?> del(@RequestBody @Validated(BaseRequest.delete .class) SampleDataRequest request) {
       sampleDataService.removeByIdExt(request.getId());
        return new SuccessResponseData<>();
    }

    @PostResource(name = "样本数据-更新", path = "/edit")
    public SuccessResponseData<?> update(@RequestBody @Validated(BaseRequest.edit.class) SampleDataRequest sampleDataRequest){
        SampleData sampleData = new SampleData();
        BeanUtils.copyProperties(sampleDataRequest, sampleData);
         sampleDataService.updateByIdExt(sampleData);
        return new SuccessResponseData<>();
    }

    @GetResource(name = "样本数据-详情", path = "/detail",requiredLogin = false)
    public SuccessResponseData<?> detail(@Validated(BaseRequest.detail.class) SampleDataRequest request) {
        SampleData sampleData = sampleDataService.getByIdExt(request.getId());
        return new SuccessResponseData<>(sampleData);
    }

    @GetResource(name = "样本数据-列表", path = "/list")
    public SuccessResponseData<?> list( SampleDataRequest request) {
        List<SampleData> list = sampleDataService.list(Wrappers.<SampleData>lambdaQuery().eq(SampleData::getId, request.getId()));
        return new SuccessResponseData<>(list);
    }

    /**
     * 分页和搜索
     *
     * @param sampleDataRequest
     * @return
     */
    @GetResource(name = "样本数据-分页", path = "/page")
    public SuccessResponseData<PageResult<SampleData>> page( SampleDataRequest sampleDataRequest) {
        PageResult<SampleData> page = sampleDataService.findPage(sampleDataRequest);
        return new SuccessResponseData<>(page);
    }
}
